﻿using Language;
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace LargeSquareOne
{
    public partial class ShutdownProgressForm : MultiLanguageForm
    {
        private Panel contentPanel;
        private Label messageLabel;
        private ProgressBar progressBar;

        public ShutdownProgressForm()
        {
            // 确保先初始化组件
            InitializeComponents();
        }

        private void InitializeComponents()
        {
            // 首先创建 messageLabel
            messageLabel = new Label
            {
                Text = "正在关闭程序，请稍候...",
                AutoSize = false,
                TextAlign = ContentAlignment.MiddleCenter,
                Dock = DockStyle.Top,
                Height = 30,
                Font = new Font("Microsoft YaHei", 9F, FontStyle.Regular),
                BackColor = Color.Transparent
            };

            // 设置窗体属性
            this.FormBorderStyle = FormBorderStyle.None;
            this.StartPosition = FormStartPosition.CenterScreen;
            this.Size = new Size(300, 100);
            this.BackColor = Color.Black;
            this.Opacity = 0.5;
            this.ShowInTaskbar = false;

            // 创建内容面板
            contentPanel = new Panel
            {
                BackColor = Color.FromArgb(240, 240, 240),
                Size = new Size(280, 80),
                Dock = DockStyle.None,
                Region = new Region(GetRoundedRect(new RectangleF(0, 0, 280, 80), 8))
            };

            contentPanel.Location = new Point(
                (this.ClientSize.Width - contentPanel.Width) / 2,
                (this.ClientSize.Height - contentPanel.Height) / 2
            );

            progressBar = new ProgressBar
            {
                Style = ProgressBarStyle.Marquee,
                MarqueeAnimationSpeed = 30,
                Height = 20,
                Dock = DockStyle.None,
                Width = 250
            };

            progressBar.Location = new Point(
                (contentPanel.ClientSize.Width - progressBar.Width) / 2,
                messageLabel.Bottom + 10
            );

            // 确保按正确的顺序添加控件
            contentPanel.Controls.Add(messageLabel);
            contentPanel.Controls.Add(progressBar);
            this.Controls.Add(contentPanel);

            this.Click += (s, e) => { };
        }

        private GraphicsPath GetRoundedRect(RectangleF rect, float radius)
        {
            GraphicsPath path = new GraphicsPath();
            path.AddArc(rect.X, rect.Y, radius * 2, radius * 2, 180, 90);
            path.AddArc(rect.Width - radius * 2 + rect.X, rect.Y, radius * 2, radius * 2, 270, 90);
            path.AddArc(rect.Width - radius * 2 + rect.X, rect.Height - radius * 2 + rect.Y, radius * 2, radius * 2, 0, 90);
            path.AddArc(rect.X, rect.Height - radius * 2 + rect.Y, radius * 2, radius * 2, 90, 90);
            path.CloseFigure();
            return path;
        }

        public void UpdateMessage(string message)
        {
            // 添加空值检查
            if (messageLabel == null) return;

            if (this.InvokeRequired)
            {
                this.Invoke(new Action(() =>
                {
                    if (messageLabel != null)  // 再次检查，以确保线程安全
                        messageLabel.Text = message;
                }));
            }
            else
            {
                messageLabel.Text = message;
            }
        }

        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= 0x80000; // WS_EX_LAYERED
                cp.ExStyle |= 0x20;    // WS_EX_TRANSPARENT
                return cp;
            }
        }
    }
}
